package cobersky.skinUI.components
{
	import cobersky.skinUI.core.Container;
	import cobersky.skinUI.skins.core.IScrollerSkin;
	import cobersky.skinUI.skins.scroller.DefaultScrollerSkin;
	
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;

	public class Scroller extends Container
	{
		private var _valueX:Number;
		private var _valueY:Number;
		private var _hs:HScrollerBar;
		private var _vs:VScrollerBar;
		private var _back:DisplayObject;
		private var _maxX:Number;
		private var _maxY:Number;
		private var _showBack:Boolean;

		public function get showBack():Boolean
		{
			return _showBack;
		}

		public function set showBack(value:Boolean):void
		{
			_showBack = value;
		}

		public function get valueY():Number
		{
			return _valueY;
		}

		public function set valueY(value:Number):void
		{
			
			_valueY = value;
			_scrollRect.y=value*_maxY;
			_containerSprite.scrollRect=_scrollRect;
		}

		public function get valueX():Number
		{
			return _valueX;
		}

		public function set valueX(value:Number):void
		{
			_valueX = value;
			_scrollRect.x=value*_maxX;
			_containerSprite.scrollRect=_scrollRect;
		}
		public function Scroller()
		{
			super();
			//this._hasMask=true;
		}
		
		override public function attachSkin():void
		{
			super.attachSkin();
			const b:IScrollerSkin=new _skinClass();
			_vs=b.VScrollBar;
			_hs=b.HScrollBar;
			_back=b.backGround;
			_containerSprite=new Sprite;
			_containerSprite.x=b.defaultRectangle.x;
			_containerSprite.y=b.defaultRectangle.y;
			_vs.x=b.defaultRectangle.right;
			_vs.y=b.defaultRectangle.top;
			_vs.height=b.defaultRectangle.height;
			_hs.x=b.defaultRectangle.left;
			_hs.y=b.defaultRectangle.bottom;
			_hs.width=b.defaultRectangle.width;
			_scrollRect=b.defaultRectangle;
		}
		
		override public function create():void
		{
			super.create();
			_maxX=_containerSprite.width-_scrollRect.width;
			_maxY=_containerSprite.height-_scrollRect.height;
			_hs.enable=(_maxX>0);
			_vs.enable=(_maxY>0);
			_containerSprite.scrollRect=_scrollRect;
			addChild(_back);
			addChild(_containerSprite);
			addChild(_vs);
			addChild(_hs);
		}
		override protected function init():void
		{
			if(!_skinClass)_skinClass=DefaultScrollerSkin;
			attachSkin();
			_hs.addEventListener(Event.CHANGE,hsChange);
			_vs.addEventListener(Event.CHANGE,vsChange);
			super.init();
		}
		
		protected function vsChange(event:Event):void
		{
			this.valueY=_vs.value;
		}
		
		protected function hsChange(event:Event):void
		{
			this.valueX=_hs.value;
		}
	}
}